const DataBaseconnections = require('../app/database')
class MomentService {
  async createMoment(userId, content) {
    const statement = `INSERT INTO moment(content,user_id) VALUES(?,?);`
    //   1.将用户评论插入数据库中
    const results = await DataBaseconnections.execute(statement, [content, userId])
    console.log(results)
    if (!results[1]) {
      return '发布评论成功'
    } else {
      return results
    }
  }
  async getMomentById(momentId) {
    const statement = `SELECT
                            moment.id id,
                            moment.content content,
                            moment.createAt createTime,
                            moment.updateAt updateTime,
                            JSON_OBJECT('id',users.id,'name',users.name) author
                        FROM
                            moment
                            LEFT JOIN users ON moment.user_id = users.id 
                        WHERE
                            moment.id = ?;`
    //   1.将用户评论插入数据库中
    const results = await DataBaseconnections.execute(statement, [momentId])
    return results[0][0]
  }
  async getMonmentList(offset, size) {
    const statement = `	SELECT
                            moment.id id,
                            moment.content content,
                            moment.createAt createTime,
                            moment.updateAt updateTime,
                            JSON_OBJECT('id',users.id,'name',users.name) user
                        FROM
                            moment
                            LEFT JOIN users ON moment.user_id = users.id 
                            LIMIT ?, ?;`
    //   1.将用户评论插入数据库中
    const results = await DataBaseconnections.execute(statement, [offset, size])
    return results[0]
  }
  async updateMoment(content, momentId) {
    const statement = `UPDATE moment SET content = ?  WHERE id=?;`
    //   1.将用户评论插入数据库中
    const results = await DataBaseconnections.execute(statement, [content, momentId])
    if (!results[1]) {
      return true
    } else {
      return false
    }
  }
  async deleteMoment(momentId) {
    const statement = `
      DELETE FROM moment WHERE id=?;
      `
    const results = await DataBaseconnections.execute(statement, [momentId])
    if (!results[1]) {
        return true
      } else {
        return false
      }
  }
}
module.exports = new MomentService()
